//
//  DistanceSensor.m
//  测试demo
//
//  Created by ycx on 2019/7/2.
//  Copyright © 2019 ycx. All rights reserved.
//

#import "DistanceSensor.h"
#import <CoreMotion/CoreMotion.h>


@interface DistanceSensor()

@end

@implementation DistanceSensor
-(instancetype)init
{
    if(self == [super init])
    {
       // [self initialization];
    }
    return self;
}
-(void)initialization
{
    // 打开距离传感器
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    // 通过通知监听有物品靠近还是离开
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
}
-(void)proximityStateDidChange:(NSNotification *)note
{
        if ([UIDevice currentDevice].proximityState) {
            if(self.block)
            {
                self.block(YES);
            }
            
            NSLog(@"有物品靠近");
        } else {
            if(self.block)
            {
                self.block(NO);
            }
            NSLog(@"有物品离开");
        }
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
